#ifndef SESSION_H
#define SESSION_H
#include <string.h>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/array.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/write.hpp>
#include <iostream>
#include <map>

#include "frame.h"
#include "typetransfer.h"
#include "utility.hpp"

#include "predef.h"
using namespace std;
using boost::asio::ip::tcp;
using boost::asio::ip::address;


class Session  : public boost::enable_shared_from_this<Session>
{
public:
    Session(boost::asio::io_service &io_service) : socket_(io_service)//shared_ptr<AsynSocketServer> server, server_(server),
    {
    }

    void Start()
    {
        static tcp::no_delay option(true);
        socket_.set_option(option);
        socket_.async_read_some(boost::asio::buffer(frame.recv_data),
                                boost::bind(&Session::HandleReadCb,
                                            shared_from_this(),
                                            boost::asio::placeholders::error,
                                            boost::asio::placeholders::bytes_transferred));
    }
    boost::system::error_code Write(const u_char *data, const size_t len)
    {
        boost::system::error_code ec;
        //socket_.write_some(boost::asio::buffer(data,len), ec);
        return ec;
    }
    tcp::socket& GetSocket(){ return socket_; }

    void HandleDataCb(const u_char *data,const size_t len);
private:
    void HandleWriteCb(const boost::system::error_code& error, size_t bytes_transferred)
    {
        cout<<"async write handle"<<endl;
        if(error)
        {
            cout<<"error:"<<error.message()<<endl;
            return;
        }
    }
    void HandleReadCb(const boost::system::error_code& error, size_t bytes_transferred);


//private:
    tcp::socket socket_;
    Frame frame;
};

typedef boost::shared_ptr<Session> SessionPtr;

#endif // SESSION_H
